<template>
  <div class="box">
    <div class="head-fixed">
      <div class="head">
        <div class="head-div">
          <img src="../../assets/zjt2.png" alt="" class="back" @click="back()" />
        </div>
        <div class="head-search" v-show="status == 0">个人信息</div>
        <div class="head-search" v-show="status == 1">招聘者信息</div>
        <div @click="saveIndividual">保存</div>
      </div>
    </div>
    <div class="content">
      <div class="inpBox">
        <span>头像</span>
        <input v-show="false" type="file" accept="image/*" @change="tirggerFile($event)" ref="input"/>
        <div>
          <img :src="imgUrl" v-if="company_user.userPortrait" alt=""  @click="openImg"/>
          <img src="../../assets/avatar.png" v-else alt="" @click="openImg"/>
          <!--          不存在显示空-->
        </div>
      </div>
      <div class="inpBox">
        <span>姓名</span>
        <div>
          <input type="text" v-model="company_user.userName" placeholder="请输入姓名"/>
        </div>
      </div>
      <div class="inpBox">
        <span>性别</span>
        <div>
          <input type="radio" name="sex" v-model="company_user.sex" value="0"/><span>男</span>
          <input type="radio" name="sex" v-model="company_user.sex" value="1"/><span>女</span>
        </div>
      </div>
      <div class="inpBox">
        <span>职位</span>
        <div>
          <input type="text" v-model="company_user.userPosition" placeholder="请输入职位"/>
        </div>
      </div>
      <div class="inpBox">
        <span>电话</span>
        <div>
          <input type="text" v-model="company_user.mobile" placeholder="请输入电话号码"/>
        </div>
      </div>
      <div class="inpBox">
        <span>邮箱</span>
        <div>
          <input type="text" v-model="company_user.email" placeholder="请输入邮箱"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import {Toast} from 'mint-ui'

export default {
  inject: ['reload'],
  name: 'companyInformation',
  components: {},
  created () {
    // created 在模板渲染成html前调用，即通常初始化某些属性值，然后再渲染成视图
  },
  mounted () {
    // mounted在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
    // eslint-disable-next-line camelcase,no-unused-vars
    // eslint-disable-next-line camelcase
    this.company_user.companyId = localStorage.getItem('cache_company_id')
    this.company_user.id = localStorage.getItem('userId')

    let that = this
    this.$http({
      method: 'get',
      url: Vue.prototype.AppURL + '/comuser/get/company/user/data',
      params: {id: this.company_user.id}
    }).then(({ data }) => {
      if (data) {
        that.company_user = data

        that.imgUrl = Vue.prototype.AppURL + '/file/' + data.userPortrait
      }
    })
  },
  // data() 为变量赋值等
  data () {
    return {
      imgUrl: '',
      // status为用户身份，0是求职者，1是招聘者
      status: localStorage.status,
      company_user: {
        companyId: null,
        userPortrait: '',
        userName: '',
        sex: '',
        userPosition: '',
        mobile: '',
        email: '',
        id: ''
      }
    }
  },
  // methods 编写js函数
  methods: {
    notEmpty (val) {
      const re = new RegExp('^[ ]+$')
      return !(val == null || val === '' || typeof (val) === 'undefined' || re.test(val))
    },
    saveIndividual () {
      if (!this.notEmpty(this.company_user.userPortrait)) {
        return Toast('请上传头像')
      }
      if (!this.notEmpty(this.company_user.userName)) {
        return Toast('请输入姓名')
      }
      if (!this.notEmpty(this.company_user.sex)) {
        return Toast('请选择性别')
      }
      if (!this.notEmpty(this.company_user.userPosition)) {
        return Toast('请输入职位')
      }
      if (!this.notEmpty(this.company_user.mobile)) {
        return Toast('请输入手机号码')
      }
      if (this.company_user.mobile.length !== 11) {
        return Toast('请输入正确的手机号码')
      }
      if (!this.notEmpty(this.company_user.email)) {
        return Toast('请输入邮箱')
      }
      let that = this
      this.$http({
        method: 'post',
        url: Vue.prototype.AppURL + '/comuser/update/individual/data',
        data: this.company_user
      }).then(({ data }) => {
        console.log(data) //在means里已经缓存中，存入了id，id已经绑定到浏览器了，不需要后端在传过来数据了
        //means里之所以有2个数据传递过来是因为那两个是下拉，是动态获取的
        if (data) {
          Toast('修改成功')
          this.$router.push({name: 'company'})
        } else {
          return Toast('修改失败')
        }
      })
    },
    back () {
      this.$router.push({name: 'companyMy'})
    },
    tirggerFile: function (event) {//event是弹出的头像选择
      let file = event.target.files[0]//获取选中的第一个文件，（因为上传主键可以选择多个图片）
      let url = ''
      var reader = new FileReader()//文件流
      reader.readAsDataURL(file) //这个文件通过文件流形式解析
      let that = this//重载vue对象。把全局付给局部，在onload方法使用
      reader.onload = function (e) {//文件读取结束后回调
        url = this.result.substring(this.result.indexOf(',') + 1)//获取文件的base64
        that.imgUrl = 'data:image/png;base64,' + url//回掉展示上传图片
        that.company_user.userPortrait = url
      }
    },
    openImg () {
      this.$refs.input.click()
    }
  }
}
</script>

<style scoped>
html,
body,
.tac,
.box,
.tab-left,
.tab-left .el-menu {
  height: 100%;
}

.box {
  width: 100%;
  background: #fff;
}

.head-fixed {
  width: 100%;
  height: 43px;
  position: fixed;
  z-index: 999;
}

.head {
  background: #3e63a8;
  color: #fff;
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
}

.back {
  width: 14px;
  height: 14px;
}

.head div img {
  width: 20px;
  height: 20px;
}

.head-search {
  box-sizing: border-box;
  flex: 1;
  text-align: center;
  font-size: 14px;
}

.content {
  position: relative;
  top: 43px;
  width: 100%;
  box-sizing: border-box;
  padding: 0 20px;
}

.inpBox {
  width: 100%;
  box-sizing: border-box;
  display: flex;
  border-bottom: 1px solid #eee;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  font-size: 14px;
}

.inpBox img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

.inpBox input {
  border: 0;
  flex: 1;
  outline: none;
  font-size: 14px;
  height: 30px;
  text-align: right;
}

.inpBox div {
  display: flex;
  align-items: center;
}

.inpBox div input[type="radio"] {
  margin-left: 20px;
  margin-right: 5px;
}

.inpBoxs {
  width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-flow: column;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.inpBoxs .inpBox {
  border: 0;
}

.inpBoxs textarea {
  width: 100%;
  height: 150px;
  margin-top: 10px;
  box-sizing: border-box;
  padding: 10px;
}
</style>
